// =========设置axios以及后端地址=============
import glo_loading from "./loading"
import {apiLoading } from "../common/js/loadingConfig";
import axios from 'axios'
import {router} from "../main";
import {Message,Loading} from 'element-ui'

export const service = axios.create({
    /*baseURL: '/api', // 请求前缀url*/
    baseURL:"http://localhost:8085",
    withCredentials: true, // send cookies when cross-domain requests
    timeout: 50000 // 超时时间
})

export default {
    fetchPost,
    fetchGet,
}
//返回一个Promise(发送post请求)
export function fetchPost(url, params) {
    return new Promise((resolve, reject) => {
        service.post(url, params)
            .then(response => {
                resolve(response);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error)
            })
    })
}
//返回一个Promise(发送get请求)
export function fetchGet(url, param) {
    return new Promise((resolve, reject) => {
        service.get(url, {params: param})
            .then(response => {
                resolve(response)
            }, err => {
                reject(err)
            })
            .catch((error) => {
                reject(error)
            })
    })
}

service.interceptors.request.use(
    config => {
        //设置统一token
       let token = localStorage.getItem("token");
       if(token){
           config.headers.token = token;
       }
       return config;
    },
    error => {
        // 请求失败之后，隐藏遮罩
        return Promise.reject(error)
    }
)
service.interceptors.response.use(
    response => {
        let result = response.data
        if (!result.success && result.message === "noLogin"){
            router.push("/login");
            response.data.message="请登陆后访问！"
            return response;
        } else if (!result.success && result.message === "noAuth"){
            response.data.message="尚未授权进行访问！"
            router.push("/403")
            return response;
        }
        // 消息回显
        if(result.success&&result.message!=null) {
            Message.success(result.message);
        } else if(!result.success&&result.message!=null) {
            Message.error(result.message);
        }
        return response;
    },
)

/*
// 遮罩层 1
service.interceptors.request.use(
    config => {
        // 只要请求，就打开遮罩
        apiLoading ? glo_loading.loadingShow() : ''
        // console.log(config)
        return config;
    },
    error => {
        // 请求失败之后，隐藏遮罩
        apiLoading ? glo_loading.loadingHide() : ''
        console.log(error) // for debug
        return Promise.reject(error)
    }
)
service.interceptors.response.use(
    response => {
        // 如果是Blob类型，直接返回response
        if (response.data instanceof Blob) {
            apiLoading ? glo_loading.loadingHide() : ''
            return response
        }
        if (response.headers['content-disposition']) {
            apiLoading ? glo_loading.loadingHide() : ''
            return response
        }
        console.log(response);
        apiLoading ? glo_loading.loadingHide() : ''
        return response;
    },
    error => {
        console.log('err' + error) // for debug
        apiLoading ? glo_loading.loadingHide() : ''
        return Promise.reject(error)
    }
)*/
/*
// 遮罩层 2
var ser;
service.interceptors.request.use(
    config=> {
    ser = Loading.service({text:"Loading..."});
    return config;
}, err=> {
    Message.error({message: '请求超时!'});
    return Promise.resolve(err);
})
//  响应拦截
service.interceptors.response.use(res=> {
    ser.close();
    if (res.status === 200) {
        return res;
    }
    return Promise.reject(res);
}, err=> {
    ser.close();
    if (err.response.status == 504||err.response.status == 404) {
        Message.error({message: '服务器被吃了⊙﹏⊙∥'});
    } else if (err.response.status == 403) {
        Message.error({message: '权限不足,请联系管理员!'});
    } else {
        Message.error({message: '未知错误'});
    }
    return Promise.reject(err);
})
*/
